<?php
namespace Admin\Controller;

use Lib\Controller\AdminBaseController;
class ServerController extends AdminBaseController{
    
    private $serverNameModel;
    public function __construct(){
        $this->serverNameModel = D('serverName');
        parent::__construct();
    }
    /**
     * 显示所有服务项目
     */
    public function server_name_list(){
        $serverNameList = $this->serverNameModel->select();
        $this->assign('list',$serverNameList);
//         p($serverNameList);
        $this->display();
    }
    /**
     * 添加新的服务项目
     */
    public function add_server_name(){
        $data['server_name'] = I('post.server_name');
        $data['server_type'] = I('post.server_type');
        $data['create_time']  = time();
        $data['update_time'] = time();
        $data['status'] = 1;
        $addServerName = $this->serverNameModel->add($data);
        if($addServerName){
            $info['status'] = 1;
            $info['info'] = '添加成功';
            $info['url'] = U(MODULE_NAME.'/server/server_name_list');
            $this->ajaxReturn($info);
        }else{
            $info['status'] = 0;
            $info['info'] = '添加失敗';
            $this->ajaxReturn($info);
        }
    }
    /**
     * 修改狀態
     */
    public function change_server_status(){
        $where['id'] = I('get.id');
        $result = $this->serverNameModel->where($where)->find();
        
        $status = $result['status'] > 0 ? 0 : 1;
        $updateServerName = $this->serverNameModel->where($where)->save(array('status'=>$status,'update_time'=>time()));
        if($updateServerName){
            $info['status'] = 1;
            $info['info'] = '修改成功';
            $info['url'] = U(MODULE_NAME.'/server/server_name_list');
            $this->ajaxReturn($info);
        }else{
            $info['status'] = 0;
            $info['info'] = '修改失敗';
            $this->ajaxReturn($info);
        }
    }
    /**
     * 刪除服務
     */
    public function delete_server() {
        $where['id'] = I('get.id');
        
        $deleteServer = $this->serverNameModel->where($where)->delete();
        if($deleteServer){
            $info['status'] = 1;
            $info['info'] = '刪除成功';
            $info['url'] = U(MODULE_NAME.'/server/server_name_list');
            $this->ajaxReturn($info);
        }else{
            $info['status'] = 0;
            $info['info'] = '刪除失敗';
            $this->ajaxReturn($info);
        }
    }
}